package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mybatis.spring.annotation.MapperScan;
import org.py.mymodule.submodule.common.Result;
import org.py.mymodule.submodule.entity.Journal;
import org.py.mymodule.submodule.entity.Journalform;
import org.py.mymodule.submodule.mapper.JournalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.BitSet;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/journal")
@MapperScan("org.py.mymodule.submodule.mapper")
public class JournalController {
    @Autowired
    private JournalMapper journalMapper;
    @GetMapping("/getJournal")
    public List<Journal> getJournal(){
        List<Journal> journals = journalMapper.selectList(null);
        return journals;

    }

    @PostMapping("/add")
    public Result add(@RequestBody Journalform journalform) {

        LocalDate localDate = LocalDate.now();
        Date sqlDate = Date.valueOf(localDate);
        Journal journal = new Journal();

        // 验证 logger_name 是否为空
        if (journalform.getLoggerName() == null || journalform.getLoggerName().isEmpty()) {
            return Result.error("logger_name 不能为空");
        }

        // 设置内容，如果为空则设置为 "无"
        journal.setContents(journalform.getContents().isEmpty() ? "无" : journalform.getContents());

        // 设置 logger_name
        journal.setLoggerName(journalform.getLoggerName());

        // 设置日期
        journal.setLogDate(sqlDate);

        // 插入数据
        journalMapper.insert(journal);

        return Result.success(journal);
    }


    @GetMapping("/selectbydate")
    public List<Journal> selectbydate(@RequestParam String date){
        // 将日期字符串转换为 java.util.Date 类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sqlDate = Date.valueOf(date);

        QueryWrapper<Journal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("log_date", sqlDate.toString());
        List<Journal> journals = journalMapper.selectList(queryWrapper);
        return journals;
    }


}
